package ch.nortgram.countdown_worldcup_2014.fragment;

import java.net.HttpURLConnection;
import java.net.URI;
import java.util.ArrayList;
import java.util.Locale;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import ch.nortgram.countdown_worldcup_2014.R;

public class RequestFragment extends Fragment
	{
	View request_view;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
		{
		request_view = inflater.inflate(R.layout.activity_request,null);
		return request_view;
	    }
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState)
		{
		super.onActivityCreated(savedInstanceState);
		((Button)request_view.findViewById(R.id.btn_request_submit)).setOnClickListener(new OnClickListener()
			{
			@Override
			public void onClick(View v)
				{
				new reqeust_online().execute();
				}
			});
		}
	
	
    public class reqeust_online extends AsyncTask<Void, Integer, Boolean>
    	{
    	
    	@Override
		protected void onPostExecute(Boolean result)
			{
    		Toast.makeText(getActivity(), "Thanks for your submit", Toast.LENGTH_SHORT).show();
			}
	
		@Override
		protected Boolean doInBackground(Void... params)
			{
    		/* Prepare Variables  */
    		int request_widget = 0;
    		if(((CheckBox)request_view.findViewById(R.id.chx_request_widget)).isChecked())
    			{
    			request_widget = 1;
    			}
    		int request_gallery = 0;
    		if(((CheckBox)request_view.findViewById(R.id.chx_request_gallery)).isChecked())
    			{
    			request_gallery = 1;
    			}
    		int request_sound = 0;
    		if(((CheckBox)request_view.findViewById(R.id.chx_request_sound)).isChecked())
    			{
    			request_sound = 1;
    			}
    		/*  
			*/
			HttpClient client = new DefaultHttpClient();
	        HttpPost post = new HttpPost(URI.create((getResources().getString(R.string.url_request_send))));	// Ziel des HTTP Requests
	        try {
	            ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
	            nameValuePair.add(new BasicNameValuePair("request_widget", request_widget +""));
	            nameValuePair.add(new BasicNameValuePair("request_gallery", request_gallery +""));
	            nameValuePair.add(new BasicNameValuePair("request_sound", request_sound +""));
	            nameValuePair.add(new BasicNameValuePair("request_custom", ((EditText)request_view.findViewById(R.id.edt_request)).getText().toString()));
	            nameValuePair.add(new BasicNameValuePair("action", "1"));
	            // Debug information
	            nameValuePair.add(new BasicNameValuePair("device_country", Locale.getDefault().getDisplayCountry()));
	            nameValuePair.add(new BasicNameValuePair("device_language", Locale.getDefault().getLanguage()));
	            nameValuePair.add(new BasicNameValuePair("device_model", android.os.Build.MODEL));
	            nameValuePair.add(new BasicNameValuePair("device_manufacturer", android.os.Build.MANUFACTURER));
	            // Debug information
	            post.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
	            HttpResponse response = client.execute(post);
	            StatusLine statusLine = response.getStatusLine();
	            if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK)
	            	{
	                return true;
	            	}
	        	}
	        catch (Exception e) {}
	        return false;
			}
		}
   
    
	@Override
	public void onPause()
		{
		super.onPause();
		}
	
	@Override
	public void onResume()
		{
		super.onResume();
		}
	}